package paketFK;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.widget.Toast;

@SuppressWarnings("rawtypes")
public class NetCheckLogin extends AsyncTask {
	private Activity activity;
	private ProgressDialog nDialog;

	/**
	 * @uml.property name="processLogin"
	 * @uml.associationEnd multiplicity="(1 1)"
	 *                     inverse="netCheckLogin:paketFK.ProcessLogin"
	 * @uml.association name="startet"
	 */
	private ProcessLogin processLogin = null;

	@Override
	protected Boolean doInBackground(Object... params) {
		ConnectivityManager cm = (ConnectivityManager) activity
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnected()) {
			try {
				URL url = new URL("http://www.google.com"); //$NON-NLS-1$
				HttpURLConnection urlc = (HttpURLConnection) url
						.openConnection();
				urlc.setConnectTimeout(3000);
				urlc.connect();
				if (urlc.getResponseCode() == 200) {
					return true;
				}
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onPostExecute(Object th) {
		if ((Boolean) th == true) {
			nDialog.dismiss();
			processLogin = new ProcessLogin();
			processLogin.setActivity(activity);
			processLogin.execute();

			// Toast.makeText(activity.getApplicationContext(),
			// "Network Connection successful", Toast.LENGTH_SHORT).show();

		} else {
			nDialog.dismiss();
			Toast.makeText(activity.getApplicationContext(),
					"Network Connection failed", Toast.LENGTH_SHORT).show(); //$NON-NLS-1$
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		nDialog = new ProgressDialog(activity);
		nDialog.setTitle("Checking Network"); //$NON-NLS-1$
		nDialog.setMessage("Loading..."); //$NON-NLS-1$
		nDialog.setIndeterminate(false);
		nDialog.setCancelable(true);
		nDialog.show();
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

}
